package ext.tianma.bom.service;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import wt.change2.WTChangeOrder2;
import wt.log4j.LogR;
import ext.tianma.bom.bean.BOMBean;
import ext.tianma.bom.eco.BOMECOUpdate;
import ext.tianma.bom.eco.BOMHelp;
import ext.tianma.bom.eco.ECOCRUDUtil;
import ext.tianma.bom.eco.ECOHelp;
import ext.tianma.changeManager.service.CheckService;

@Controller
@RequestMapping({ "/ECOChange" })
public class ECOChangeService {
	@Autowired
	HttpServletRequest request;
	
	@Autowired
	CheckService checkservice;
	
	private String firstValidate = "";
	private static final Logger log = LogR.getLogger(ECOChangeService.class.getName());
	/**
	 * 用量校验
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/dosModificCheck", method = { RequestMethod.POST,
			RequestMethod.GET })
	public void dosModificCheck(@RequestBody String data,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		System.out.println("=========dosModificCheck  data:"+data);
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		// 逻辑处理
		try {
			JSONObject obj = new JSONObject(data);
			if (obj.has("datalist")) {
				JSONArray array = new JSONArray(obj.getString("datalist"));
				result = BOMHelp.updatePartAmountValidate(array);
				log.info("===============amount result:" + result);
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
		} catch (Exception e) {
			log.info(e);
			log.info("================" + e);
			result = TMECOUtil.errorUpdateMessage(e.getMessage());
		}
		out.print(result);
	}
	/**
	 * 搜索受影响对象
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/tmEcoSingleSearch", method = {
			RequestMethod.POST, RequestMethod.GET })
	public void tmEcoSingleSearch(@RequestParam(value = "param", required = true) String param,
			@RequestParam(value = "type", required = false) String type,
			HttpServletResponse response) throws Exception {
		// CredentialsProvider credsProvider = new BasicCredentialsProvider();
		// credsProvider.setCredentials(new AuthScope(uri.getHost(),
		// uri.getPort()), new
		// UsernamePasswordCredentials("orgadmin", "orgadmin"));
		/*
		 * request.getRequestURI() credsProvider.setCredentials(new
		 * AuthScope(uri.getHost(), uri.getPort()), new
		 * UsernamePasswordCredentials("wcadmin", "wcadmin"));
		 * CloseableHttpClient httpclient =
		 * HttpClients.custom().setDefaultCredentialsProvider
		 * (credsProvider).build();
		 */
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		String param222 = request.getParameter("param");
		log.info("======param =========" + param222);
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		// 逻辑处理
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		try {
			JSONObject obj = new JSONObject(param);
			if(obj.has("datalist") && "1".equals(type)){
				JSONArray array = new JSONArray(obj.getString("datalist"));
				result = BOMHelp.queryMutiEffectPart(array);
			}else if (obj.has("datalist")) {
				JSONArray array = new JSONArray(obj.getString("datalist"));
				result = BOMHelp.queryEffectPart(array);
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
		} catch (Exception e) {
			log.info(e);
			log.info("================" + e);
			result = TMECOUtil.errorUpdateMessage(e.getMessage());
		}finally{
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
		out.print(result);
	}
	
	/**
	 * 用量修改提交
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/dosModific", method = { RequestMethod.POST,
			RequestMethod.GET })
	public void dosModific(
			@RequestBody String data,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		// 逻辑处理
		try {
			JSONObject obj = new JSONObject(data);
			String oids = obj.getString("ecoid");
			String type = obj.getString("type");
			String effectnumber = obj.getString("effectnumber");
			WTChangeOrder2 eco = (WTChangeOrder2) BOMHelp.REF_FACTORY
					.getReference(oids).getObject();
			if (eco != null) {
				ECOCRUDUtil.deleteECO(eco.getNumber(), effectnumber,
						BOMBean.UpdatePart, type);
			}
			if (obj.has("datalist")) {
				JSONArray array = new JSONArray(obj.getString("datalist"));
				result = BOMECOUpdate.updatePartBom(array);
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
		} catch (Exception e) {
			log.info(e);
			log.info("================" + e);
			result = TMECOUtil.errorUpdateMessage(e.getMessage());
		}finally{
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
		out.print(result);
	}
	/**
	 * 新增物料校验
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/tmAddCheck", method = { RequestMethod.POST,
			RequestMethod.GET })
	public void tmAddCheck(
			@RequestBody String data,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		// 逻辑处理
		try {
			JSONObject obj = new JSONObject(data);
			if (obj.has("datalist")) {
				JSONArray array = new JSONArray(obj.getString("datalist"));
				result = BOMHelp.addPartValidate(array);
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
		} catch (Exception e) {
			log.info(e);
			log.info("================" + e);
			result = TMECOUtil.errorUpdateMessage(e.getMessage());
		}
		out.print(result);
	}
	
	/**
	 * 新增物料提交
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/tmAddNum", method = { RequestMethod.POST,
			RequestMethod.GET })
	public void tmAddNum(
			@RequestBody String data,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		// 逻辑处理
		try {
			JSONObject obj = new JSONObject(data);
			String oids = obj.getString("ecoid");
			String type = obj.getString("type");
			String effectnumber = obj.getString("effectnumber");
			WTChangeOrder2 eco = (WTChangeOrder2) BOMHelp.REF_FACTORY
					.getReference(oids).getObject();
			if (eco != null) {
				ECOCRUDUtil.deleteECO(eco.getNumber(), effectnumber,
						BOMBean.ADDPart, type);
			}
			if (obj.has("datalist")) {
				JSONArray array = new JSONArray(obj.getString("datalist"));
				result = BOMECOUpdate.addToBom(array);
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
		} catch (Exception e) {
			log.info(e);
			log.info("================" + e);
			result = TMECOUtil.errorUpdateMessage(e.getMessage());
		}finally{
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
		out.print(result);
	}
	
	/**
	 * 添加替代料
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/addReplacePart", method = { RequestMethod.POST,
			RequestMethod.GET })
	public void addReplacePart(
			@RequestBody String data,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		// 逻辑处理
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		try {
			JSONObject obj = new JSONObject(data);
			String oids = obj.getString("ecoid");
			String type = obj.getString("type");
			String effectnumber = obj.getString("effectnumber");
			WTChangeOrder2 eco = (WTChangeOrder2) BOMHelp.REF_FACTORY
					.getReference(oids).getObject();
			if (eco != null) {
				ECOCRUDUtil.deleteECO(eco.getNumber(), effectnumber,
						BOMBean.ADDReplcePart, type);
			}
			if (obj.has("datalist")) {
				JSONArray array = new JSONArray(obj.getString("datalist"));
				result = BOMECOUpdate.addReplacePartBOM(array);
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
		} catch (Exception e) {
			log.info(e);
			log.info("================" + e);
			result = TMECOUtil.errorUpdateMessage(e.getMessage());
		}finally{
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
		out.print(result);
	}
	/**
	 * 新增替代料校验
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/addReplacePartCheck", method = {
			RequestMethod.POST, RequestMethod.GET })
	public void addReplacePartCheck(
			@RequestBody String data,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		// 逻辑处理
		try {
			JSONObject obj = new JSONObject(data);
			if (obj.has("datalist")) {
				JSONArray array = new JSONArray(obj.getString("datalist"));
				result = BOMHelp.addReplacePartBOMValidate(array);
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
		} catch (Exception e) {
			log.info(e);
			log.info("===================" + e);
			result = TMECOUtil.errorUpdateMessage(e.getMessage());
		}
		out.print(result);
	}
	
	/**
	 * 查询BOM下零件批准情况
	 * 返回   需要零件承认的零件信息
	 * @param number
	 * @param view
	 * @param response
	 * @throws Exception
	 */
	/*@RequestMapping(value = "/tmBOMSearch", method = { RequestMethod.POST,
			RequestMethod.GET })
	public void tmBOMSearch(
			@RequestParam(value = "number", required = true) String number,
			@RequestParam(value = "view", required = false) String view,
			HttpServletResponse response) throws Exception {
		PrintWriter out = response.getWriter();
		response.setContentType("application/json; charset=UTF-8");
		// 用户名密码校验
		String result = "";
		result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}
		// 逻辑处理
		result = BOMHelp.searhPart(number, view);
		out.print(result);
	}*/
	
	/**
	 * 删除物料校验
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/tmdeleteCheck", method = { RequestMethod.POST,
			RequestMethod.GET })
	public void tmdeleteCheck(
			@RequestBody String data,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		// 逻辑处理
		try {
			JSONObject obj = new JSONObject(data);
			if (obj.has("datalist")) {
				JSONArray array = new JSONArray(obj.getString("datalist"));
				result = BOMHelp.deletePartValidate(array);
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
		} catch (JSONException e) {
			log.info(e);
			result = TMECOUtil.errorUpdateMessage(e.getMessage());
		}
		out.print(result);
	}

	/**
	 * 删除物料提交
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/tmdelete", method = { RequestMethod.POST,
			RequestMethod.GET })
	public void tmdelete(
			@RequestBody String data,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		// 逻辑处理
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		try {
			JSONObject obj = new JSONObject(data);
			String oids = obj.getString("ecoid");
			String type = obj.getString("type");
			String effectnumber = obj.getString("effectnumber");
			WTChangeOrder2 eco = (WTChangeOrder2) BOMHelp.REF_FACTORY
					.getReference(oids).getObject();
			if (eco != null) {
				ECOCRUDUtil.deleteECO(eco.getNumber(), effectnumber,
						BOMBean.DelPart, type);
			}
			if (obj.has("datalist")) {
				JSONArray array = new JSONArray(obj.getString("datalist"));
				result = BOMECOUpdate.deletaPartBom(array);
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
		} catch (JSONException e) {
			log.info(e);
			result = TMECOUtil.errorUpdateMessage(e.getMessage());
		}finally{
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
		out.print(result);
	}
	
	/**
	 * 删除替换物料提交
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/deleteReplacePart", method = {
			RequestMethod.POST, RequestMethod.GET })
	public void deleteReplacePart(
			@RequestBody String data,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		// 逻辑处理
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		try {
			JSONObject obj = new JSONObject(data);
			String oids = obj.getString("ecoid");
			String type = obj.getString("type");
			String effectnumber = obj.getString("effectnumber");
			WTChangeOrder2 eco = (WTChangeOrder2) BOMHelp.REF_FACTORY
					.getReference(oids).getObject();
			if (eco != null) {
				ECOCRUDUtil.deleteECO(eco.getNumber(), effectnumber,
						BOMBean.DelReplcePart, type);
			}
			if (obj.has("datalist")) {
				JSONArray array = new JSONArray(obj.getString("datalist"));
				result = BOMECOUpdate.deleteReplacePartBOM(array);
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
		} catch (JSONException e) {
			log.info(e);
			result = TMECOUtil.errorUpdateMessage(e.getMessage());
		}finally{
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
		out.print(result);
	}
	/**
	 * 删除替代料校验
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/deleteReplacePartCheck", method = {
			RequestMethod.POST, RequestMethod.GET })
	public void deleteReplacePartCheck(
			@RequestBody String param,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		// 逻辑处理
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		try {
			JSONObject obj = new JSONObject(param);
			if (obj.has("datalist")) {
				JSONArray array = new JSONArray(obj.getString("datalist"));
				result = BOMHelp.deleteReplacePartBOMValidate(array);
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
		} catch (JSONException e) {
			log.info(e);
			result = TMECOUtil.errorUpdateMessage(e.getMessage());
		}finally{
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
		out.print(result);
	}
	/**
	 * 替换主料提交
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/repalcePartA", method = { RequestMethod.POST,
			RequestMethod.GET })
	public void repalcePartA(
			@RequestBody String param,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		// 逻辑处理
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		try {
			JSONObject obj = new JSONObject(param);
			String oids = obj.getString("ecoid");
			String type = obj.getString("type");
			String effectnumber = obj.getString("effectnumber");
			WTChangeOrder2 eco = (WTChangeOrder2) BOMHelp.REF_FACTORY
					.getReference(oids).getObject();
			if (eco != null) {
				ECOCRUDUtil.deleteECO(eco.getNumber(), effectnumber,
						BOMBean.UpdateAPart, type);
			}
			if (obj.has("datalist")) {
				JSONArray array = new JSONArray(obj.getString("datalist"));
				result = BOMECOUpdate.repalcePartA(array);
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
		} catch (JSONException e) {
			log.info(e);
			result = TMECOUtil.errorUpdateMessage(e.getMessage());
		}finally{
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
		out.print(result);
	}
	/**
	 * 主次替换提交
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/repalcePartAAndPartB", method = {
			RequestMethod.POST, RequestMethod.GET })
	public void repalcePartAAndPartB(
			@RequestBody String param,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		// 逻辑处理
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		try {
			JSONObject obj = new JSONObject(param);
			String oids = obj.getString("ecoid");
			String type = obj.getString("type");
			String effectnumber = obj.getString("effectnumber");
			WTChangeOrder2 eco = (WTChangeOrder2) BOMHelp.REF_FACTORY
					.getReference(oids).getObject();
			if (eco != null) {
				ECOCRUDUtil.deleteECO(eco.getNumber(), effectnumber,
						BOMBean.ReplaceABPart, type);
			}
			if (obj.has("datalist")) {
				JSONArray array = new JSONArray(obj.getString("datalist"));
				result = BOMECOUpdate.repalcePartAAndPartB(array);
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
		} catch (JSONException e) {
			log.info(e);
			result = TMECOUtil.errorUpdateMessage(e.getMessage());
		}finally{
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
		out.print(result);
	}
	/**
	 * 主次替换校验
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/repalcePartAAndPartBCheck", method = {
			RequestMethod.POST, RequestMethod.GET })
	public void repalcePartAAndPartBCheck(
			@RequestBody String param,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		// 逻辑处理
		try {
			JSONObject obj = new JSONObject(param);
			if (obj.has("datalist")) {
				JSONArray array = new JSONArray(obj.getString("datalist"));
				result = BOMHelp.repalcePartAAndPartBValidate(array);
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
		} catch (JSONException e) {
			result = TMECOUtil.errorUpdateMessage(e.getMessage());
		}
		out.print(result);
	}
	
	/**
	 * 替换主料校验
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/repalcePartACheck", method = {
			RequestMethod.POST, RequestMethod.GET })
	public void repalcePartACheck(
			@RequestBody String param,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		// 逻辑处理
		try {
			JSONObject obj = new JSONObject(param);
			if (obj.has("datalist")) {
				JSONArray array = new JSONArray(obj.getString("datalist"));
				result = BOMHelp.repalcePartAValidate(array);
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
		} catch (JSONException e) {
			log.info(e);
			result = TMECOUtil.errorUpdateMessage(e.getMessage());
		}
		out.print(result);
	}
	/**
	 * 替换物料校验
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/tmReplaceCheck", method = { RequestMethod.POST,
			RequestMethod.GET })
	public void tmReplaceCheck(
			@RequestBody String param,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		// 逻辑处理
		try {
			JSONObject obj = new JSONObject(param);
			if (obj.has("datalist")) {
				JSONArray array = new JSONArray(obj.getString("datalist"));
				result = BOMHelp.replacePartBomValidate(array);
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
		} catch (JSONException e) {
			log.info(e);
			result = TMECOUtil.errorUpdateMessage(e.getMessage());
		}
		out.print(result);
	}
	
	/**
	 * 更换替代料
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/replaceOldPart", method = { RequestMethod.POST,
			RequestMethod.GET })
	public void replaceOldPart(
			@RequestBody String param,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		// 逻辑处理
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		try {
			JSONObject obj = new JSONObject(param);
			String oids = obj.getString("ecoid");
			String type = obj.getString("type");
			String effectnumber = obj.getString("effectnumber");
			WTChangeOrder2 eco = (WTChangeOrder2) BOMHelp.REF_FACTORY
					.getReference(oids).getObject();
			if (eco != null) {
				ECOCRUDUtil.deleteECO(eco.getNumber(), effectnumber,
						BOMBean.UpdateReplcePart, type);
			}
			if (obj.has("datalist")) {
				JSONArray array = new JSONArray(obj.getString("datalist"));
				result = BOMECOUpdate.replaceOldPartBom(array);
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
		} catch (JSONException e) {
			log.info(e);
			result = TMECOUtil.errorUpdateMessage(e.getMessage());
		}finally{
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
		out.print(result);
	}
	/**
	 * 更换替代料 校验
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/replaceOldPartCheck", method = {
			RequestMethod.POST, RequestMethod.GET })
	public void replaceOldPartCheck(
			@RequestBody String param,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		// 逻辑处理
		try {
			JSONObject obj = new JSONObject(param);
			if (obj.has("datalist")) {
				JSONArray array = new JSONArray(obj.getString("datalist"));
				result = BOMHelp.replaceOldPartBomValidate(array);
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
		} catch (JSONException e) {
			log.info(e);
			result = TMECOUtil.errorUpdateMessage(e.getMessage());
		}
		out.print(result);
	}
	
	/**
	 * 替代料用量修改校验
	 * @param param
	 * @param response
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2021年1月25日
	 */
	@RequestMapping(value = "/addSubstitutesQuantityCheckValidate", method = {RequestMethod.POST, RequestMethod.GET })
	public void addSubstitutesQuantityCheckValidate(
			@RequestBody String param,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		// 逻辑处理
		try {
			JSONObject obj = new JSONObject(param);
			if (obj.has("datalist")) {
				JSONArray array = new JSONArray(obj.getString("datalist"));
				result = checkservice.addSubstitutesQuantityCheckValidate(array).toString();
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
		} catch (JSONException e) {
			log.info(e);
			result = TMECOUtil.errorUpdateMessage(e.getMessage());
		}
		out.print(result);
	}
	
	/**
	 * 替代料用量修改提交
	 * @param param
	 * @param response
	 * @throws Exception
	 * @author xucr
	 * @CreateDate 2021年1月25日
	 */
	@RequestMapping(value = "/addSubstitutesQuantity", method = {RequestMethod.POST, RequestMethod.GET })
	public void addSubstitutesQuantity(
			@RequestBody String param,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		String result = "";
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		// 逻辑处理
		try {
			JSONObject obj = new JSONObject(param);
			String oids = obj.getString("ecoid");
			String type = obj.getString("type");
			String effectnumber = obj.getString("effectnumber");
			WTChangeOrder2 eco = (WTChangeOrder2) BOMHelp.REF_FACTORY.getReference(oids).getObject();
			if (eco != null) {
				ECOCRUDUtil.deleteECO(eco.getNumber(), effectnumber, BOMBean.UpdateSubstitutesQuantity, type);
			}
			if (obj.has("datalist")) {
				JSONArray array = new JSONArray(obj.getString("datalist"));
				result = BOMECOUpdate.addSubstitutesQuantity(array);
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
		} catch (Exception e) {
			log.info(e);
			log.info("================" + e);
			result = TMECOUtil.errorUpdateMessage(e.getMessage());
		}finally{
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
		out.print(result);
	}

	/**
	 * 替换物料提交
	 * 
	 * @param param
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/tmReplaceSubmit", method = { RequestMethod.POST,
			RequestMethod.GET })
	public void tmReplaceSubmit(
			@RequestBody String param,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		// 逻辑处理
		boolean enforce = wt.session.SessionServerHelper.manager.setAccessEnforced(false);
		try {
			JSONObject obj = new JSONObject(param);
			String oids = obj.getString("ecoid");
			String type = obj.getString("type");
			String effectnumber = obj.getString("effectnumber");
			WTChangeOrder2 eco = (WTChangeOrder2) BOMHelp.REF_FACTORY
					.getReference(oids).getObject();
			if (eco != null) {
				ECOCRUDUtil.deleteECO(eco.getNumber(), effectnumber,
						BOMBean.ReplacePart, type);
			}
			if (obj.has("datalist")) {
				JSONArray array = new JSONArray(obj.getString("datalist"));
				result = BOMECOUpdate.replacePartBom(array);
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
		} catch (JSONException e) {
			log.info(e);
			result = TMECOUtil.errorUpdateMessage(e.getMessage());
		}finally{
			wt.session.SessionServerHelper.manager.setAccessEnforced(enforce);
		}
		out.print(result);
	}
	/**
	 * ECO 查询清单 界面中根据ECO编号查找变更的受影响对象
	 * @param econumber
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryECOByNumber", method = { RequestMethod.POST,
			RequestMethod.GET })
	public void queryECOByNumber(
			@RequestParam(value = "econumber", required = true) String econumber,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		// 逻辑处理
		result = ECOHelp.queryECOHistoryByNumber(econumber);
		out.print(result);
	}

	/**
	 * ECO变更清单查询接口
	 * @param data key :value
	 * startdate
	 * enddate
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/tmSearchECO", method = { RequestMethod.POST,
			RequestMethod.GET })
	public void tmSearchECO(
			@RequestParam(value = "data", required = true) String data,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		// 逻辑处理 数据格式{key:"",value:""}
		try {
			JSONObject obj = new JSONObject(data);
			if (obj.has("key") && obj.has("value") &&  obj.has("startdate") &&  obj.has("enddate")) {
				String sdate = obj.getString("startdate");
				String edate = obj.getString("enddate");
				String key = obj.getString("key");
				String value = obj.getString("value");
				result = BOMHelp.queryECOOrder(key, value,sdate,edate);
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
		} catch (Exception e) {
			log.info(e);
			result = TMECOUtil.errorUpdateMessage(e.getMessage());
		}
		out.print(result);
	}

	/**
	 * header校验
	 * 
	 * @return
	 */
	public String firstValidate() {
		log.info("======request===" + request);
		String req_username = request.getHeader("username");
		String req_pwd = request.getHeader("password");
		log.info("user:" + req_username);
		log.info("pwd:" + req_pwd);
		firstValidate = TMECOUtil.userValidate(req_username, req_pwd);
		log.info("firstValidate:" + firstValidate);
		if (!isNullOrEmpty(firstValidate)) {
			return firstValidate;
		}
		return "";
	}

	public static boolean isNullOrEmpty(String str) {
		return (str == null || "".equals(str) || str.equals("null") || str
				.equals("NULL"));
	}

	/**
	 * 单个变更 ECO变更清单查询 根据ECO oid 返回本次变更的受影响对象
	 * 
	 * @param data
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/tmECOHistoryData", method = { RequestMethod.POST,
			RequestMethod.GET })
	public void tmECOHistoryData(
			@RequestParam(value = "oid", required = true) String oid,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		// 逻辑处理
		result = ECOHelp.queryECOHistory(oid);
		out.print(result);
	}

	/**
	 * 根据ECO编号及受影响对象编号、变更类型 查找记录
	 * 
	 * @param econumber
	 * @param effectnumber
	 * @param operatetype
	 * @param type   0：单个变更   1：批量变更   其他：ECO变更查询
	 * 
	 * return：0：返回单个变更中指定受影响对象的某种变更类型数据
	 * 		   1：返回批量变更的某种变更数据
	 * 		  其他：返回ECO变更查询中指定受影响对象的某种变更类型数据
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/tmECODataDetail", method = { RequestMethod.POST,
			RequestMethod.GET })
	public void tmECODataDetail(
			@RequestParam(value = "oid", required = true) String oid,
			@RequestParam(value = "econumber", required = true) String econumber,
			@RequestParam(value = "effectnumber", required = true) String effectnumber,
			@RequestParam(value = "view", required = true) String view,
			@RequestParam(value = "operatetype", required = true) String operatetype,
			@RequestParam(value = "type", required = true) String type,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		// 逻辑处理
		result = ECOHelp.queryECOOrderbyType(oid,econumber, effectnumber, operatetype,
				type,view);
		out.print(result);
	}

	/**
	 * 单个变更 删除变更对象 根据ECO oid 返回本次变更的受影响对象
	 * 
	 * @param data
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/tmECODeleteSingleData", method = {
			RequestMethod.POST, RequestMethod.GET })
	public void tmECODeleteSingleData(
			@RequestParam(value = "oid", required = true) String oid,
			@RequestParam(value = "number", required = true) String number,
			@RequestParam(value = "view", required = true) String view,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		// 逻辑处理
		result = ECOHelp.deleteSingleEffectData(oid, number,view);
		out.print(result);
	}
	
	/**
	 *根据ECO oid或编号查询本次变更的类型及相应的数量
	 * 
	 * @param data
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/tmSearchECOType", method = {
			RequestMethod.POST, RequestMethod.GET })
	public void tmSearchECOType(
			@RequestParam(value = "econumber", required = true) String econumber,
			@RequestParam(value = "effectnumber", required = true) String effectnumber,
			@RequestParam(value = "view", required = true) String view,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		/*result = firstValidate();
		if (!isNullOrEmpty(result)) {
			out.print(result);
			return;
		}*/
		// 逻辑处理
		result = ECOCRUDUtil.queryECOtype(econumber, effectnumber,view);
		out.print(result);
	}
	/**
	 * ECO变更清单根据受影响对象编号查询接口
	 * @param partNumber
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/tmSearchECOByPartNumber", method = { RequestMethod.POST,
			RequestMethod.GET })
	public void tmSearchECOByProductNumber(
			@RequestParam(value = "partNumber", required = true) String partNumber,
			HttpServletResponse response) throws Exception {
		response.setContentType("application/json; charset=UTF-8");
		PrintWriter out = response.getWriter();
		// 用户名密码校验
		String result = "";
		// 逻辑处理 数据格式{key:"",value:""}
		try {
			if (!"".equals(partNumber) && null!=partNumber) {
				result = BOMHelp.searchECOData(partNumber);
			} else {
				result = TMECOUtil.errorRequestMessage();
			}
		} catch (Exception e) {
			log.info(e);
			result = TMECOUtil.errorUpdateMessage(e.getMessage());
		}
		out.print(result);
	}
	/**
	 * 导出ECO报表
	 * @param data
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/exportECOReport", method = { RequestMethod.POST,
			RequestMethod.GET })
	public void exportECOReport(@RequestBody String data, HttpServletResponse response) throws Exception{
		PrintWriter out = response.getWriter();
		String datalist = request.getParameter("datalist");
		System.out.println("======datalist =========" + datalist);
		System.out.println("======data =========" + data);
		response.setContentType("text/html; charset=UTF-8");
		String result = "";
		// 逻辑处理
		try {
			JSONObject object = new JSONObject(data);
			if(object.has("datalist")){
				JSONArray array = (JSONArray)object.get("datalist");
				result = ECOHelp.exportECOReport(array);
			}
		} catch (Exception e) {
			System.out.println("======exportECOReport Exception:"+e);
			result = TMECOUtil.errorRequestMessage();
			log.debug(e.getMessage());
		}

		out.print(result);
	}
	/**
	 * 删除ECO批量变更中某一变更类型数据接口
	 * @param changetype
	 * @param oid
	 * @param ecotype
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping(value = "/tmDeleteECOChangeDetail", method = { RequestMethod.POST,
			RequestMethod.GET })
	public void tmDeleteECOChangeDetail(@RequestParam(value = "changetype", required = true) String changetype,
			@RequestParam(value = "oid", required = true) String oid,
			@RequestParam(value = "ecotype", required = true) String ecotype,
			HttpServletResponse response) throws Exception{
		PrintWriter out = response.getWriter();
		response.setContentType("text/html; charset=UTF-8");
		boolean result = false;
		// 逻辑处理
		try {
			String econumber = "";
			WTChangeOrder2 eco = (WTChangeOrder2) BOMHelp.REF_FACTORY
					.getReference(oid).getObject();
			if(eco!=null){
				econumber = eco.getNumber();
			}
				result = ECOCRUDUtil.deleteECOChangeType1(econumber, changetype);
		} catch (Exception e) {
			System.out.println("======tmDeleteECOChangeDetail Exception:"+e.getMessage());
			log.debug(e.getMessage());
		}

		out.print(result);
	}
}
